﻿#ifndef WORKER_H
#define WORKER_H

#include <QObject>
#include "WordEngine.h"
#include <windows.h>
#include <QThread>
#include <QDateTime>
#include <QApplication>
#include <QDir>
#include <QDebug>

class Worker : public QObject
{
    Q_OBJECT
public:
    explicit Worker(QObject *parent = nullptr);
    ~Worker();

    int countDocxFiles(const QString &directoryPath) {
        int docxCount = 0;
        QDir dir(directoryPath);

        if (!dir.exists()) {
            qDebug() << "Directory does not exist:" << directoryPath;
            return 0;
        }

        // 获取目录下所有文件和子目录
        QFileInfoList list = dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot | QDir::NoSymLinks);

        foreach (QFileInfo fileInfo, list) {
            if (fileInfo.isDir()) {
                // 如果是子目录，递归调用
                docxCount += countDocxFiles(fileInfo.absoluteFilePath());
            } else if (fileInfo.suffix().toLower() == "docx") {
                // 如果是.docx文件，计数增加
                docxCount++;
            }
        }

        return docxCount;
    }
public slots:
    void doWork(QPair<QString, QString> recode,QStringList _list)
    {
        HRESULT r = OleInitialize(0);
        if (r != S_OK && r != S_FALSE)
        {
            qWarning("Qt:初始化Ole失败（error %x）",(unsigned int)r);
        }
        QString m_strFilePath = QStringLiteral("%1/template/处分文件.docx").arg(QApplication::applicationDirPath());
        QString temp = QStringLiteral("%1/template/处分文件temp.docx").arg(QApplication::applicationDirPath());
        QString tempPDF = QStringLiteral("%1/template/处分文件temp.pdf").arg(QApplication::applicationDirPath());
        QString history = QStringLiteral("%1/History/").arg(QApplication::applicationDirPath());
        QDate curdate = QDate::currentDate();
        WordEngine m_pWordEngine;
        m_pWordEngine.open(m_strFilePath,false);
        m_pWordEngine.setMarks("shuqian0",curdate.toString("yyyy"));
        m_pWordEngine.setMarks("shuqian00",QString::number(countDocxFiles(history)+1));
        m_pWordEngine.setMarks("shuqian1",_list.at(0));
        m_pWordEngine.setMarks("shuqian2",recode.second);
        m_pWordEngine.setMarks("shuqian3",_list.at(0));
        m_pWordEngine.setMarks("shuqian4",_list.at(3));
        QStringList CSData = _list.at(4).split('-');
        m_pWordEngine.setMarks("shuqian5",CSData.at(0));
        m_pWordEngine.setMarks("shuqian6",CSData.at(1));
        m_pWordEngine.setMarks("shuqian7",CSData.at(2));
        m_pWordEngine.setMarks("shuqian8",_list.at(2));
        QStringList RDData = _list.at(1).split('-');
        m_pWordEngine.setMarks("shuqian9",RDData.at(0));
        m_pWordEngine.setMarks("shuqian10",RDData.at(1));
        m_pWordEngine.setMarks("shuqian11",_list.at(5));
        m_pWordEngine.setMarks("shuqian12",_list.at(6));
        m_pWordEngine.setMarks("shuqian13",curdate.toString("yyyy"));
        m_pWordEngine.setMarks("shuqian14",curdate.toString("MM"));
        m_pWordEngine.setMarks("shuqian15",curdate.toString("dd"));
        m_pWordEngine.setMarks("shuqian16",_list.at(0));
        m_pWordEngine.setMarks("shuqian17",recode.first.remove('\n'));
        m_pWordEngine.setMarks("shuqian18",_list.at(0));
        int index = recode.first.indexOf(QStringLiteral("条"));
        QString num = "";
        if(index < 8)
        {
            num = recode.first.mid(1,index-1);
        }
        m_pWordEngine.setMarks("shuqian19",num);
        m_pWordEngine.setMarks("shuqian20",_list.at(0));
        m_pWordEngine.setMarks("shuqian21",recode.second);
        QString times = "";
        QDate endtime;
        if(recode.second == QStringLiteral("警告"))
        {
            times = QStringLiteral("一年");
            endtime = curdate.addMonths(12);
        }
        else if(recode.second == QStringLiteral("严重警告"))
        {
            times = QStringLiteral("一年半");
            endtime = curdate.addMonths(18);
        }
        else if(recode.second == QStringLiteral("留党察看"))
        {
            times = QStringLiteral("一年");
            endtime = curdate.addMonths(12);
        }
        else if(recode.second == QStringLiteral("开除党籍"))
            times = QStringLiteral("无期限");
        m_pWordEngine.setMarks("shuqian22",times);
        m_pWordEngine.setMarks("shuqian23",curdate.toString("yyyy"));
        m_pWordEngine.setMarks("shuqian24",curdate.toString("MM"));
        m_pWordEngine.setMarks("shuqian25",curdate.toString("dd"));
        m_pWordEngine.setMarks("shuqian26",curdate.toString("yyyy"));
        m_pWordEngine.setMarks("shuqian27",curdate.toString("MM"));
        m_pWordEngine.setMarks("shuqian28",curdate.toString("dd"));
        m_pWordEngine.setMarks("shuqian29",endtime.toString("yyyy"));
        m_pWordEngine.setMarks("shuqian30",endtime.toString("MM"));
        m_pWordEngine.setMarks("shuqian31",endtime.toString("dd"));
        m_pWordEngine.setMarks("shuqian32",curdate.toString("yyyy"));
        m_pWordEngine.setMarks("shuqian33",curdate.toString("MM"));
        m_pWordEngine.setMarks("shuqian34",curdate.toString("dd"));
        m_pWordEngine.setMarks("shuqian35",_list.at(6));
        m_pWordEngine.setMarks("shuqian36",_list.at(0));
        m_pWordEngine.setMarks("shuqian37",curdate.toString("yyyy"));
        m_pWordEngine.setMarks("shuqian38",curdate.toString("MM"));
        m_pWordEngine.setMarks("shuqian39",curdate.toString("dd"));
        m_pWordEngine.saveAs(temp);
        m_pWordEngine.close();

        QFile file(tempPDF);
        if (file.exists()) {
            if (file.remove()) {
            }
        }
        QAxObject word("Word.Application");
        word.setProperty("Visible", false);
        QAxObject* documents = word.querySubObject("Documents");
        QAxObject* document = documents->querySubObject("Open(QString)", temp);
        QVariant OutputFileName(tempPDF);
        QVariant ExportFormat(17);
        QVariant OpenAfterExport(false);
        document->querySubObject("ExportAsFixedFormat(const QVariant&,const QVariant&,const QVariant&)",
                                 OutputFileName,
                                 ExportFormat,
                                 OpenAfterExport);
        document->dynamicCall("Close(boolean)",false);
        word.dynamicCall("Quit (void)");

        OleUninitialize();
        emit resultReady("");
    }

    void doWork2()
    {
        HRESULT r = OleInitialize(0);
        if (r != S_OK && r != S_FALSE)
        {
            qWarning("Qt:初始化Ole失败（error %x）",(unsigned int)r);
        }
        QString temp = QStringLiteral("%1/template/处分文件temp.docx").arg(QApplication::applicationDirPath());
        QAxObject word("Word.Application");
        word.setProperty("Visible", false);
        QAxObject* documents = word.querySubObject("Documents");
        QAxObject* document = documents->querySubObject("Open(QString)", temp);
        QString strSaveFile = QStringLiteral("%1/History/处分文件%2.docx")
                .arg(QApplication::applicationDirPath())
                .arg(QDateTime::currentDateTime().toString("yyyyMMddhhmmss"));
        document->dynamicCall("SaveAs (const QString&)", strSaveFile);
        document->dynamicCall("PrintOut()");
        document->dynamicCall("Close(boolean)",false);
        word.dynamicCall("Quit (void)");
        OleUninitialize();
    }
signals:
    void resultReady(const QString &result);
};

#endif // WORKER_H
